﻿#ifndef __SS_THREAD_H__
#define __SS_THREAD_H__

#ifdef SS_USE_BOOST_THREAD
#include <boost/thread.hpp>
typedef void(*ssThreadFunc_t)();
#elif defined _WIN32
#include <windows.h>
#include <process.h>
typedef _beginthreadex_proc_type ssThreadFunc_t;
typedef HANDLE ssThreadHandle_t;
#else
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
typedef void *(*ssThreadFunc_t) (void *);
typedef pthread_t ssThreadHandle_t;
#endif

namespace StarSeeker
{

class ssThread
{
public:
	ssThread();
	virtual ~ssThread();
	//启动线程
	bool StartThread(ssThreadFunc_t func);
	//等待退出线程
	void WaitForExit();
	//是否运行中
	bool IsRunning() const;
	//获取线程句柄
	ssThreadHandle_t GetHandle();
	//设置线程名
	void SetName(const char* name, const wchar_t* wname);
	//线程重新调度，重新调度调用此函数的线程
	static bool Yields();
    //睡眠毫秒，睡眠调用此函数的线程
	static void Sleep(unsigned int ms);
	//获取当前线程句柄
	static ssThreadHandle_t GetCurrentHandle();
	//设置当前线程名
	static void SetCurrentName(const char* name, const wchar_t* wname);

protected:
#ifdef SS_USE_BOOST_THREAD
	boost::thread	*	m_hThread;
#else
	ssThreadHandle_t	m_hThread;
#endif
public:
	unsigned int		m_ThreadId;
};

}
#endif
